// pages/myevaluation/myevaluation.js
const app = getApp()
var url = require('../../utils/url.js');
var token ;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderGoods:"",
    order:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     var item=options.item;
     var order = JSON.parse(item)
     console.log(order)
     var order_data = order.order_data;
     for (var i = 0; i < order_data.length; i++) {
      var flag = "flag"
       order_data[i].flag = 0;
       order_data[i].filesImg = [];
       order_data[i].files=[];
     }
     this.setData({
       order:order,
       orderGoods: order_data
     })
     wx.getStorage({
       key: 'token',
       success: function(res) {
         token=res.data
       },
     })
  },
  bindTextAreaChange: function (e) {
    // console.log(e)
    var that = this
    var index = e.currentTarget.dataset.index;
    var orderGoods = that.data.orderGoods;
    orderGoods[index].info = e.detail.value;
    // var value = e.detail.value;
    that.setData({
      orderGoods: orderGoods
    })
  },
  changeColor1: function (e) {
    var that = this;
    var orderGoods = that.data.orderGoods
    var index = e.currentTarget.dataset.index;
    orderGoods[index].flag = 1
    that.setData({
      orderGoods: orderGoods
    });
  },
  changeColor2: function (e) {
    var that = this;
    var orderGoods = that.data.orderGoods
    var index = e.currentTarget.dataset.index;
    orderGoods[index].flag = 2
    that.setData({
      orderGoods: orderGoods
    });
  },
  changeColor3: function (e) {
    var that = this;
    var orderGoods = that.data.orderGoods
    var index = e.currentTarget.dataset.index;
    orderGoods[index].flag = 3
    that.setData({
      orderGoods: orderGoods
    });
  },
  changeColor4: function (e) {
    var that = this;
    var orderGoods = that.data.orderGoods
    var index = e.currentTarget.dataset.index;
    orderGoods[index].flag = 4
    that.setData({
      orderGoods: orderGoods
    });
  },
  changeColor5: function (e) {
    var that = this;
    var orderGoods = that.data.orderGoods
    var index = e.currentTarget.dataset.index;
    orderGoods[index].flag = 5
    that.setData({
      orderGoods: orderGoods
    });
  },
  chooseImage: function (e) {
    var that = this;
    var index=e.currentTarget.dataset.index;
    var orderGoods = that.data.orderGoods;
    wx.chooseImage({
      count: 9, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        // success
        console.log(res)
        var uploudImg = res.tempFilePaths[0];


        wx.uploadFile({
          url: url.URl + 'file/upload',
          filePath: uploudImg,
          name: 'file',
          formData: { type: 1 },
          header: {
            "Content-Type": "multipart/form-data",
            'Authorization': token
          },
          success: function (res) {
            console.log(res)
            var data = JSON.parse(res.data)
            console.log(data)
            console.log(url)
            var newwImg = url.URl2 + data.data
            console.log(newwImg)
            if (data.code == 200) {
              wx.showToast({
                title: '图片上传成功',
              })
              
              orderGoods[index].filesImg = orderGoods[index].filesImg.concat(data.data),  
              orderGoods[index].files = orderGoods[index].files.concat(newwImg),  
              console.log(orderGoods)
              that.setData({
                orderGoods: orderGoods
              })
            } else {

            }

          },
          fail: function (res) {
            wx.hideToast();
            wx.showModal({
              title: '错误提示',
              content: '上传图片失败',
              showCancel: false,
              success: function (res) { }
            })
          }
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  bindSubmit:function(){
    var that=this;
    var orderGoods = that.data.orderGoods;
    var order=that.data.order;
    var orderId=order.id;
    var info=[];
    console.log(token)
    for (var i = 0; i < orderGoods.length;i++){
      var infoArray = {};
      infoArray.goods_id = orderGoods[i].goods_id;
      infoArray.goods_spec = orderGoods[i].spec_goods_id;
      infoArray.goods_evaluation_star = orderGoods[i].flag;
      infoArray.images = orderGoods[i].filesImg.join(",");
      infoArray.content = orderGoods[i].info;
      info.push(infoArray)
    }
    // var infoJson = JSON.stringify(info)

    // console.log(infoJson)
    wx.request({
      url: url.URl + 'my/order/evaluation',
      data: { id: orderId, info: info },
      method: 'post',
      header: {
        'Authorization': token
      },
      success: function (res) {
        var msg=res.data.msg;
        wx.showToast({
          title: msg,
          success: function(res) {
            wx.navigateBack({
              delta: 1,
            })
          },
          fail: function(res) {},
          complete: function(res) {},
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})